import api from '../../api';
import { message } from 'antd';
import moment from 'moment';

export default {
  namespace: 'bondArtificialTradesM',
  state: {
    historyDataObj: {}, //历史行情数据
    xAxis: [], //信用债 点状图x轴
    echartsData: [], //信用债  点状图数据
    min: undefined, //信用债 点状图y轴最小值
    max: undefined, //信用债 点状图y轴最大值
    kLineChartsData: [], //利率债  k线图数据
    zzhaiTableDataObj: {}, //中债表格数据
    zzhengTableDataObj: {}, //中债表格数据
    shqTableDataObj: {}, //上清表格数据
  },

  effects: {
    //查询历史行情数据
    *getHistorical({ payload }, { call, put }) {
      const response = yield call(api.bondBaseInfo.queryBondQuote, payload);
      if (response) {
        if (response.code == 0) {
          const data = response.data || {};
          if (!Object.keys(data).length) {
            //无数据
            yield put({
              type: 'setState',
              payload: {
                historyDataObj: {},
              },
            });
            return;
          }
          const { columns = [], dataSource = [] } = data.result || {};

          //转换成echarts需要的数据
          if (data.bondType == 1) {
            //债券类型：0-利率债 1-信用债
            const arrX = data.dateList || []; //获取x轴刻度
            const echartsData = []; //散点图data
            const arrPrice = []; //价格合集
            let max = dataSource[0] ? dataSource[0].price * 1 : 100;
            let min = dataSource[0] ? dataSource[0].price * 1 : 0;
            arrX.map((date, key) => {
              dataSource.map((item, index) => {
                item.price * 1 > max ? (max = item.price * 1) : null;
                item.price * 1 < min ? (min = item.price * 1) : null;
                item.key = index;
                let price = item.price * 1;
                arrPrice.push(price);
                if (item.date == date) {
                  let tempArr = [key, price];
                  echartsData.push(tempArr);
                }
              });
            });
            yield put({
              type: 'setState',
              payload: {
                xAxis: arrX, //信用债 点状图x轴
                echartsData: echartsData, //信用债  点状图数据
                min: min,
                max: max,
              },
            });
          } else {
            // series数组中每个元素的内容：['time', "open", "close", 'lowest', 'highest'] （即：[开盘值, 收盘值, 最低值, 最高值]）
            const arr = []; //转换成k线图的格式
            dataSource.map((item, index) => {
              item.key = index;
              const temp = {};
              temp['time'] = moment(item.date).format('YYYY/MM/DD');
              temp['open'] = item.open;
              temp['close'] = item.close;
              temp['lowest'] = item.low;
              temp['highest'] = item.high;
              arr.unshift(temp);
            });
            yield put({
              type: 'setState',
              payload: {
                kLineChartsData: arr,
              },
            });
          }
          yield put({
            type: 'setState',
            payload: {
              historyDataObj: data,
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              historyDataObj: {},
              xAxis: [], //信用债 点状图x轴
              echartsData: [], //信用债  点状图y轴
              kLineChartsData: [],
              min: undefined,
              max: undefined,
            },
          });
          message.error(response.msg || '查询历史行情失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            historyDataObj: {},
            xAxis: [], //信用债 点状图x轴
            echartsData: [], //信用债  点状图y轴
            kLineChartsData: [],
            min: undefined,
            max: undefined,
          },
        });
        message.error('查询历史行情失败!');
      }
    },
    //查询异常交易记录
    *queryBondTradeList({ payload }, { call, put }) {
      const response = yield call(api.bondBaseInfo.queryBondAbnormalTrade, payload);
      const { type } = payload;
      if (response) {
        if (response.code == 0) {
          const data = response.data || {};
          if (type === 0) {
            //0-中债，1-上清，2-中证
            yield put({
              type: 'setState',
              payload: {
                zzhaiTableDataObj: data,
              },
            });
          } else if (type === 1) {
            yield put({
              type: 'setState',
              payload: {
                shqTableDataObj: data,
              },
            });
          } else {
            yield put({
              type: 'setState',
              payload: {
                zzhengTableDataObj: data,
              },
            });
          }
        } else {
          if (type === 0) {
            //0-中债，1-上清，2-中证
            yield put({
              type: 'setState',
              payload: {
                zzhaiTableDataObj: {},
              },
            });
          } else if (type === 1) {
            yield put({
              type: 'setState',
              payload: {
                zzhengTableDataObj: {},
              },
            });
          } else {
            yield put({
              type: 'setState',
              payload: {
                shqTableDataObj: {},
              },
            });
          }
          message.error(response.msg || '查询异常交易记录失败!');
        }
      } else {
        message.error('查询异常交易记录失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
